package sisi.vatarticles;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * VatarticleController.java - Controller class for the <i>VAT articles database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 19/11/2013
 * 
 */

public class VatarticleController { 

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Vatarticle[] getVatarticle(String cerca) {
		List<Vatarticle> vatarticle = getVatarticleList(cerca);
		return vatarticle.toArray(new Vatarticle[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Vatarticle> getVatarticleList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.articleNumber) ) LIKE :cerca OR TRIM( BOTH FROM UPPER(c.englishDescription) )  LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Vatarticle as c " + cWhere
							+ " order by c.articleNumber");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Vatarticle> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Vatarticle getVatarticlexId(int id) {

		EntityManager em = getEntityManager();
		try {
			Vatarticle vatarticlex = em.find(Vatarticle.class, id);
			return vatarticlex;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Vatarticle addVatarticle(Vatarticle vatarticle) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(vatarticle);
			em.getTransaction().commit();
			vatarticle = refreshVatarticle(vatarticle);
		} finally {
			em.close();
			return vatarticle;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateVatarticle(Vatarticle vatarticle) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Vatarticle vatarticlex = em.find(Vatarticle.class, vatarticle.getId());
			vatarticlex.setArticleNumber(vatarticle.getArticleNumber());
			vatarticlex.setId(vatarticle.getId());
			vatarticlex.setCode(vatarticle.getCode());
			vatarticlex.setEnglishDescription(vatarticle.getEnglishDescription());
			vatarticlex.setItalianDescription(vatarticle.getItalianDescription());
			em.getTransaction().commit();
			em.flush();
		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Vatarticle refreshVatarticle(Vatarticle vatarticle) {

		Vatarticle vatarticlex = vatarticle;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			vatarticlex = em.find(Vatarticle.class, vatarticle.getId());
			em.flush();
			em.refresh(vatarticlex);
		} finally { 
			em.close();
			return vatarticlex;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeVatarticle(Vatarticle vatarticle) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Vatarticle vatarticlex = em.find(Vatarticle.class, vatarticle.getId());
			em.remove(vatarticlex);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
